import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreConfigService extends BaseService<any> {
  private readonly logger = new Logger(CoreConfigService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getConfig
   * 对应 PHP: CoreConfigService_core::getConfig()
   * 逻辑类型: undefined - undefined
   */
  async getConfig(site_id: number, key: number) {
    // 基于PHP真实逻辑: getConfig
    // PHP原文: $cache_name = 'site_config_cache';         $where = array(             [ 'config_key', '=', $key ],             [ 'site_id', '=', $site_id ]         )...
const cache_name = 'site_config_cache';
        const where = [
            [ 'config_key', '=', key ],
            [ 'site_id', '=', site_id )
        ];
        // 缓存清理
        const info = cache_remember(
            cache_name . key  + '_" . site_id,
            function() use (where) {
                const data = this.model.where(where).field('id,site_id,config_key,value,status,create_time,update_time').findOrEmpty().toArray(];
                //数据库中无数据返回-1
                if (!data)) {
                    return -1;
                }
                return data;
  }

  /**
   * setConfig
   * 对应 PHP: CoreConfigService_core::setConfig()
   * 逻辑类型: undefined - undefined
   */
  async setConfig(site_id: any[], key: any[], value: any[]) {
    // 基于PHP真实逻辑: setConfig
    // PHP原文: $where = array(             [ 'config_key', '=', $key ],             [ 'site_id', '=', $site_id ]         );         $data = array(             'site_...
const where = [
            [ 'config_key', '=', key ],
            [ 'site_id', '=', site_id )
        ];
        const data = [
            site_id: site_id,
            config_key: key,
            value: value,
        ];
        const info = this.getConfig(site_id, key);
        if (!info)) {
            data[ 'create_time' ] = time(];
            const res = this.model.create(data);
  }

  /**
   * modifyStatus
   * 对应 PHP: CoreConfigService_core::modifyStatus()
   * 逻辑类型: undefined - undefined
   */
  async modifyStatus(site_id: number, status: number, key: number) {
    // 基于PHP真实逻辑: modifyStatus
    // PHP原文: $where = array(             [ 'config_key', '=', $key ],             [ 'site_id', '=', $site_id ]         );         $data = array(             'statu...
const where = [
            [ 'config_key', '=', key ],
            [ 'site_id', '=', site_id )
        ];
        const data = [
            status: status,
        ];
        return this.model.where(where).save(data);
    }

    /**
     * 返回config信息
     * @param int site_id
     * @param string key
     * @return array|mixed
     */
    async getConfigValue(int site_id, string key)
    {
        const config_info = this.getConfig(site_id, key);
        if (!config_info)) {
            return [];
  }

  /**
   * getConfigValue
   * 对应 PHP: CoreConfigService_core::getConfigValue()
   * 逻辑类型: undefined - undefined
   */
  async getConfigValue(site_id: number, key: number) {
    // 基于PHP真实逻辑: getConfigValue
    // PHP原文: $config_info = $this->getConfig($site_id, $key);         if (empty($config_info)) {             return [];...
const config_info = this.getConfig(site_id, key);
        if (!config_info)) {
            return [];
  }
}
